<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('新增借阅')" />
	<th:block th:include="include :: datetimepicker-css" />
	<th:block th:include="include :: select2-css" />
	<th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-library-add">
<!--			<div class="form-group">-->
<!--				<label class="col-sm-3 control-label">借阅名：</label>-->
<!--				<div class="col-sm-8">-->
<!--					<input class="form-control" type="text" name="name" id="name" required>-->
<!--				</div>-->
<!--			</div>-->
			<div class="form-group" onclick="selectUsersToParentCallBack('admin/library/book',doSubmit1)">
				<label class="col-sm-3 control-label">选择图书</label>
				<div class="col-sm-8">
					<select class="form-control select2-multiple" name="bookId" multiple id="bookId">
					</select>
				</div>
			</div>
			<div class="form-group" onclick="selectUsersToParentCallBack('system/user',doSubmit2)">
				<label class="col-sm-3 control-label">借阅用户</label>
				<div class="col-sm-8">
					<select class="form-control select2-multiple" name="userId"  id="userId">
					</select>
				</div>
			</div>
		</form>
	</div>
	<th:block th:include="include :: footer" />
	<th:block th:include="include :: datetimepicker-js" />
	<th:block th:include="include :: select2-js" />
	<th:block th:include="include :: bootstrap-select-js" />
	<script type="text/javascript">
		$(function(){
			layui.use('laydate', function(){
				var laydate = layui.laydate;

				laydate.render({
					elem: '#laydate-publishDate',
					type: 'date'
				});
			});
		});

	</script>
	<script type="text/javascript">
		// callBack获取父窗口方法（方式二）
		function selectUsersToParentCallBack(url,doSubmit1){
			var options = {
				title: '选择用户',
				url: ctx + url,
				callBack: doSubmit1
			};
			$.modal.openOptions(options);
		}


		function doSubmit1(index, layero){
			var rows = layero.find("iframe")[0].contentWindow.getSelections();
			if (rows.length == 0) {
				$.modal.alertWarning("请至少选择一条记录");
				return;
			}
			// $("#bookId option").remove();
			for (var i=0;i<rows.length;i++)
			{
				if (rows[i].count>0){
					var tyname = '<option value="'+rows[i].bookId+'">'+rows[i].name+'-'+rows[i].author+'</option>'
					$("#bookId").append(tyname);
				}
			}
			$("#bookId option").attr("selected", true);
			console.log(rows)
			$.modal.close(index);
		}
		function doSubmit2(index, layero){
			var rows = layero.find("iframe")[0].contentWindow.getSelections();
			if (rows.length == 0) {
				$.modal.alertWarning("请至少选择一条记录");
				return;
			}
			$("#userId option").remove();
			for (var i=0;i<rows.length;i++)
			{
				var username = '<option value="'+rows[i].userId+'">'+rows[i].dept.deptName+'-'+rows[i].userName+'</option>'
				$("#userId").append(username);
				$("#userId ").val(rows[i].userId);
			}
			$.modal.close(index);
		}
	</script>
	<script type="text/javascript">
		var prefix = ctx + "admin/library/borrow";


		function submitHandler() {
			if ($.validate.form()) {
				$.operate.save(prefix + "/add", $('#form-library-add').serialize());
			}
		}
	</script>
</body>
</html>
